home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2 Examples.sit / Raven 1.2 Examples / BoxPaint / Source / Texture.cpp < prev    next >
Text File  |  1997-03-09  |  4KB  |  166 lines

  1. /*
  2.  *  File:       Texture.cpp
  3.  *  Summary:    T3DPixmapTexture than can be drawn onto.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):
  10.  *
  11.  *         <->      3/09/97    JDJ        Created.
  12.  */
  13.  
  14. #include "Texture.h"
  15.  
  16. #include <QD3DStorage.h>
  17.  
  18. #include <Z3DVectors.h>
  19. #include <Z3DUtils.h>
  20.  
  21.  
  22. // ===================================================================================
  23. //    class CTextureMap
  24. // ===================================================================================
  25.  
  26. //---------------------------------------------------------------
  27. //
  28. // CTextureMap::~CTextureMap
  29. //
  30. //---------------------------------------------------------------
  31. CTextureMap::~CTextureMap()
  32. {
  33. }
  34.  
  35.  
  36. //---------------------------------------------------------------
  37. //
  38. // CTextureMap::CTextureMap
  39. //
  40. //---------------------------------------------------------------
  41. CTextureMap::CTextureMap(const TPicture& pict, ulong resolution) : T3DPixmapTexture(SPixmapStorage(pict, resolution, resolution, 32))
  42. {
  43.     mPict = pict;
  44. }
  45.  
  46.  
  47. //---------------------------------------------------------------
  48. //
  49. // CTextureMap::SetResolution
  50. //
  51. //---------------------------------------------------------------
  52. void CTextureMap::SetResolution(ulong resolution)
  53. {
  54.     if (resolution != this->GetResolution()) {
  55.         SPixmapStorage pixmap(mPict, resolution, resolution, 32);
  56.     
  57.         this->SetPixmap(pixmap);
  58.     }
  59. }
  60.  
  61.  
  62. //---------------------------------------------------------------
  63. //
  64. // CTextureMap::SetPict
  65. //
  66. //---------------------------------------------------------------
  67. void CTextureMap::SetPict(const TPicture& pict)
  68. {
  69.     mPict = pict;
  70.     
  71.     SPixmapStorage pixmap(mPict, this->GetWidth(), this->GetHeight(), 32);
  72.     
  73.     this->SetPixmap(pixmap);
  74. }
  75.  
  76.  
  77. //---------------------------------------------------------------
  78. //
  79. // CTextureMap::DrawLine (TQ3Param2D, TQ3Param2D)
  80. //
  81. //---------------------------------------------------------------
  82. void CTextureMap::DrawLine(const TQ3Param2D& fromUV, const TQ3Param2D& toUV)
  83. {
  84.     TPoint size, from, to;
  85.     
  86.     size.h = this->GetWidth();
  87.     size.v = this->GetHeight();
  88.     
  89.     from.h = size.v*fromUV.u;
  90.     from.v = size.h*(1.0 - fromUV.v);
  91.     
  92.     to.h = size.v*toUV.u;
  93.     to.v = size.h*(1.0 - toUV.v);
  94.     
  95.     this->DrawLine(from, to);
  96. }
  97.  
  98. #pragma mark ハ
  99.  
  100. //---------------------------------------------------------------
  101. //
  102. // CTextureMap::DrawLine (TPoint, TPoint)
  103. //
  104. //---------------------------------------------------------------
  105. void CTextureMap::DrawLine(const TPoint& from, const TPoint& to)
  106. {
  107.     const double kPixelSpacing    = 1.0;
  108.     const double kPixelSpacingSqr = kPixelSpacing*kPixelSpacing;
  109.     const double kMinSpacing      = 0.001;
  110.     
  111.     SPixmapStorage pixmap = this->GetPixmap();
  112.  
  113.     if (from == to)
  114.         this->SetPixel(pixmap, from);
  115.  
  116.     else {
  117.         T2DVector srcVector(to.h - from.h, to.v - from.v);
  118.  
  119.         // Find distance from old to new pixel
  120.         double pixelDistSqr = srcVector.LengthSquared();
  121.  
  122.         double spacing = kPixelSpacingSqr/pixelDistSqr;
  123.         if (spacing < kMinSpacing)
  124.             spacing = kMinSpacing;
  125.             
  126.         TPoint lastPixel(16000, 16000);
  127.  
  128.         for (double t = 0.0; t <= 1.0; t += spacing) {
  129.             TPoint pixel;
  130.             pixel.h = from.h + t*srcVector.x;
  131.             pixel.v = from.v + t*srcVector.y;
  132.  
  133.             if (pixel != lastPixel) {
  134.                 this->SetPixel(pixmap, pixel);
  135.                 lastPixel = pixel;
  136.             }
  137.         }
  138.     }
  139.  
  140.     // Q3PixmapTexture_GetPixmap returns a copy of the pixel
  141.     // image so we have to give QD the new version.
  142.     this->SetPixmap(pixmap);
  143. }
  144.  
  145.  
  146. //---------------------------------------------------------------
  147. //
  148. // CTextureMap::SetPixel
  149. //
  150. //---------------------------------------------------------------
  151. void CTextureMap::SetPixel(SPixmapStorage& pixmap, const TPoint& pixel)
  152. {
  153.     ASSERT(pixmap.pixelSize == 32);                // code assumes this is true
  154.         
  155.     long offset = 4*pixel.h + pixel.v*pixmap.rowBytes;
  156.     if (offset >= 0 && offset < pixmap.height*pixmap.rowBytes) {
  157.         long value = 0xFF000000L;                // opaque and black
  158.  
  159.         ulong bytesWritten;                
  160.         TQ3Status status = Q3Storage_SetData(pixmap.image, offset, 4, (Byte*) &value, &bytesWritten);
  161.         ThrowIf3DError(status);
  162.     }
  163. }
  164.  
  165.  
  166.